﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using System.IO;

namespace Lamex.mTools.UI
{
    public partial class frmTL_FSO_File_Operation : frmBaseChild
    {
        DirectoryInfo DirInfo;
        DataTable dt;

        public frmTL_FSO_File_Operation()
        {
            InitializeComponent();
        }

        private void frmTL_FSO_File_Operation_Load(object sender, EventArgs e)
        {
            gridControl1.UseEmbeddedNavigator = true;
            gridView1.OptionsBehavior.Editable = false;
        }

        private void btnGetPath_Click(object sender, EventArgs e)
        {
            GetProcessPath();
        }

        private void txtCurPath_DoubleClick(object sender, EventArgs e)
        {
            GetProcessPath();
        }

        #region 文件名字符串替换

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCurPath.Text) || string.IsNullOrEmpty(txtOldStr.Text))
            {
                XtraMessageBox.Show("当前路径为空，或原字符串为空！", "提示");
                return;
            }

            if (XtraMessageBox.Show("字符串为整体替换(精确匹配，区分大小写)，请再次确认替换前后字符串！" + "\r\n\r\n" + "是否确定替换？", "警告",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                return;

            int i = 0;

            string strCurPath = txtCurPath.Text;

            DirInfo = new DirectoryInfo(strCurPath);
            foreach (FileInfo fileinfo in DirInfo.GetFiles())
            {
                string strFullName = fileinfo.Name;
                int index = strFullName.LastIndexOf(".");//从右往左，指定字符（串）出现位置
                int intLen = strFullName.Length;

                string strAbbrName = strFullName.Substring(0, index);

                if (strAbbrName.Contains(txtOldStr.Text))
                {
                    // 列表中的原始文件全路径名
                    string oldStr = strCurPath + @"\" + fileinfo.Name;

                    // 新文件名
                    string newStr = strCurPath + @"\" + strAbbrName.Replace(txtOldStr.Text, txtNewStr.Text)  + fileinfo.Extension;

                    //改名方法
                    FileInfo fi = new FileInfo(oldStr);
                    fi.MoveTo(Path.Combine(newStr));
                    i++;
                }
            }
            gridControl1.DataSource = null;
            AddDataToGridview();

            txtOldStr.Text = "";
            txtNewStr.Text = "";

            XtraMessageBox.Show("共完成  " + i.ToString() + " 个文件名替换", "提示");
        }

        #endregion

        #region 文件名加前、后缀

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCurPath.Text) || (string.IsNullOrEmpty(txtPrefix.Text) && string.IsNullOrEmpty(txtSuffix.Text)))
            {
                XtraMessageBox.Show("当前路径为空，或前后缀同时为空！", "提示");
                return;
            }

            string strCurPath = txtCurPath.Text;

            DirInfo = new DirectoryInfo(strCurPath);
            foreach (FileInfo fileinfo in DirInfo.GetFiles())
            {
                string strFullName = fileinfo.Name;
                int index = strFullName.LastIndexOf(".");//从右往左，指定字符（串）出现位置
                int intLen = strFullName.Length;

                string strAbbrName = strFullName.Substring(0, index);

                // 列表中的原始文件全路径名
                string oldStr = strCurPath + @"\" + fileinfo.Name;

                // 新文件名
                string newStr = strCurPath + @"\" + txtPrefix.Text + strAbbrName + txtSuffix.Text + fileinfo.Extension;

                //改名方法
                FileInfo fi = new FileInfo(oldStr);
                fi.MoveTo(Path.Combine(newStr));

            }
            gridControl1.DataSource = null;
            AddDataToGridview();

            txtPrefix.Text = "";
            txtSuffix.Text = "";
        }

        #endregion

        private void GetProcessPath()
        {
            XtraFolderBrowserDialog folder = new XtraFolderBrowserDialog();
            //FolderBrowserDialog fb = new FolderBrowserDialog();

            //设置默认根目录是桌面
            folder.RootFolder = Environment.SpecialFolder.Desktop;
            //folder.SelectedPath = @"S:\TX-Public\TX-MFG\PPC\Material_Image";//用绝对路径时不能正确指向
            folder.SelectedPath = @"D:\Temp";

            //设置对话框说明
            folder.Description = "请选择文件搜索路径:";
            if (folder.ShowDialog() == DialogResult.OK)
            {
                txtCurPath.Text = folder.SelectedPath;
            }

            AddDataToGridview();
        }

        private void AddDataToGridview()
        {
            DirInfo = new DirectoryInfo(txtCurPath.Text);
            dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Size", typeof(int));
            dt.Columns.Add("CreationTime", typeof(DateTime));

            foreach (FileInfo fileinfo in DirInfo.GetFiles())
            {
                DataRow row = dt.NewRow();
                row["Name"] = fileinfo.FullName;
                row["Size"] = fileinfo.Length;
                row["CreationTime"] = fileinfo.CreationTime;
                dt.Rows.Add(row);
            }
            gridControl1.DataSource = dt;
        }

    }
}